! Desenvolvido por Guilherme Bertoldo e Jonas Joacir Radtke
!> \brief This program extract all the variables results listed in
!! "./input_files/varlist.txt" from all files listed in
!! "./input_files/filelist.txt" and saves the results in "./output_file.csv"

program main
   use mprint
   implicit none
   integer :: io ! input-output status
   integer :: nv ! number of variables
   integer :: nf ! number of files in the file list
   integer :: td ! Open input files ( 0 = no, 1 = yes)

   integer, parameter :: nfls = 100 ! Max. file list size
   integer, parameter :: nvls = 100 ! Max. var. list size
   integer, parameter :: nlen = 300 ! Max. string length

   character (len=nlen) str
   character (len=nlen) text_editor
   character (len=nlen) :: filelist(nfls) ! list of files
   character (len=nlen) :: varlist(nvls)  ! list of variales

   !
   ! Reading ogen options file
   !
   open(10,file="./input_files/options.txt")

   read(10,*) td
   read(10,*) text_editor

   close(10)

   !
   ! Reading file list
   !
   if ( td == 1 ) call system( trim(adjustl(text_editor)) // &
      " ./input_files/filelist.txt")

   open(10,file="./input_files/filelist.txt")
   nf = 0 ! number of files
   do
      read(10,"(A)",iostat=io) str

      if ( io /= 0 ) exit

      if ( str /= "" ) then

         nf = nf + 1

         filelist(nf) = str

      end if

   end do

   close(10)

   !
   ! Reading var list
   !
   if ( td == 1 ) call system( trim(adjustl(text_editor)) // &
      " ./input_files/varlist.txt")

   open(10,file="./input_files/varlist.txt")
   nv = 0 ! number of variables
   do
      read(10,"(A)",iostat=io) str

      if ( io /= 0 ) exit

      if ( str /= "" ) then

         nv = nv + 1

         varlist(nv) = str

      end if

   end do

   close(10)



   ! Printing in the csv format

   call print_csv( nf, nv, nlen, filelist, varlist)

   ! Printing  in the tex format

   call print_tex( nf, nv, nlen, filelist, varlist)


end program

